package com.spsoft.vatoutinv.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.controller.BaseController;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.model.bo.TokenUser;
import com.spsoft.core.model.leqidmz.param.QueryClassifyCodeParams;
import com.spsoft.vatoutinv.model.dto.vmsclassifycode.VmsClassIfyCodeLogDetailPageDto;
import com.spsoft.vatoutinv.model.dto.vmsclassifycode.VmsClassifyCodeLogBasePageDto;
import com.spsoft.vatoutinv.model.dto.vmsclassifycode.VmsClassifyCodePageDto;
import com.spsoft.vatoutinv.model.vo.vmsclassifycode.VmsClassifyCodeDetailVO;
import com.spsoft.vatoutinv.model.vo.vmsclassifycode.VmsClassifyCodeHyVO;
import com.spsoft.vatoutinv.model.vo.vmsclassifycode.VmsClassifyCodeLogBasePageVO;
import com.spsoft.vatoutinv.model.vo.vmsclassifycode.VmsClassifyCodePageVOList;
import com.spsoft.vatoutinv.service.vmsclassifycode.VmsClassifyCodeLogIndustryService;
import com.spsoft.vatoutinv.service.vmsclassifycode.VmsClassifyCodeService;
import com.spsoft.vatoutinv.service.vmsenterpriseinfo.VmsEnterpriseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 税收分类编码信息列表表 前端控制器
 * </p>
 *
 * @author wangy
 * @since 2023-11-23
 */
@Api(tags = {"税收分类编码信息查询"})
@Validated
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/vmsClassifyCode")
public class VmsClassifyCodeController extends BaseController {

    public static final String LOG_MSG = "(税收分类编码信息控制层)";

    private final VmsClassifyCodeService classifyCodeService;

    private final VmsClassifyCodeLogIndustryService industryService;

    private final VmsEnterpriseInfoService vmsEnterpriseInfoService;

    @ApiOperation("获取税收分类编码-分页查询")
    @AnonymousAccess(value = "/getCodePage", method = RequestMethod.POST, permissionCode = "VMS_TAXCLASS")
    @Log(value = "获取税收分类编码-分页查询", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXATION_CLASSIFICATION_CODE)
    public Result<IPage<VmsClassifyCodePageVOList>> getCodePage(@RequestBody @Validated VmsClassifyCodePageDto dto) {
        log.info("{}获取税收分类编码-分页查询{}", LOG_MSG, JSONObject.toJSONString(dto));
        return classifyCodeService.getCodePage(dto);
    }

    @ApiOperation("请求接口获取税收分类编码数据")
    @AnonymousAccess(value = "/addCodePage", method = RequestMethod.POST, permissionCode = "VMS_CLASS_IFY_CODE_ADD")
    @Log(value = "请求接口获取税收分类编码数据", type = LogActionType.ADD, menuName = LogMenuEnum.TAXATION_CLASSIFICATION_CODE)
    public Result<Object> getCode() {
        log.info("{}获取税收分类编码", LOG_MSG);
        TokenUser tokenUser = getUser();
        QueryClassifyCodeParams params = new QueryClassifyCodeParams();
        params.setNsrsbh(vmsEnterpriseInfoService.getNsrsbh());
        params.setCreateUser(tokenUser.getUserCode());
        params.setDeptCode(tokenUser.getDeptCode());
        return classifyCodeService.getClassifyCodeByLq(params, tokenUser);
    }

    @ApiOperation("获取税收分类编码参数-详情")
    @AnonymousAccess(value = "/getCodeDetail", method = RequestMethod.POST, permissionCode = "VMS_TAXCLASS")
    @Log(value = "获取税收分类编码参数-详情", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXATION_CLASSIFICATION_CODE)
    public Result<VmsClassifyCodeDetailVO> getCodeDetail(@RequestBody JSONObject jsonObject) {
        if (jsonObject == null || StringUtils.isBlank(jsonObject.getString("id"))) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }
        log.info("{}获取税收分类编码参数-详情，商品和服务税收分类合并编码:{}", LOG_MSG, jsonObject.getString("id"));
        return classifyCodeService.getCodeDetail(jsonObject.getString("id"));
    }

    @ApiOperation("获取记录")
    @AnonymousAccess(value = "/getBasePage", method = RequestMethod.POST, permissionCode = "VMS_TAXCLASS")
    @Log(value = "获取记录", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXATION_CLASSIFICATION_CODE)
    public Result<IPage<VmsClassifyCodeLogBasePageVO>> getBasePage(@RequestBody VmsClassifyCodeLogBasePageDto dto) {
        log.info("{}获取税收分类编码获取记录：{}", LOG_MSG, JSONObject.toJSONString(dto));
        return classifyCodeService.getBasePage(dto);
    }

    @ApiOperation("获取记录-详情列表")
    @AnonymousAccess(value = "/getBaseDetailPage", method = RequestMethod.POST, permissionCode = "VMS_TAXCLASS")
    @Log(value = "获取记录-详情列表", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXATION_CLASSIFICATION_CODE)
    public Result<IPage<VmsClassifyCodePageVOList>> getBaseDetailPage(@RequestBody VmsClassIfyCodeLogDetailPageDto dto) {
        if (StringUtils.isBlank(dto.getId())) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }
        log.info("{}获取记录-详情列表 id:{}", LOG_MSG, dto.getId());
        return classifyCodeService.getBaseDetailPage(dto);
    }

    @ApiOperation("税收分类编码导出")
    @AnonymousAccess(value = "/exportCode", method = RequestMethod.POST, permissionCode = "VMS_CLASS_IFY_CODE_EXPORT")
    @Log(value = "税收分类编码导出", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXATION_CLASSIFICATION_CODE)
    public void exportCode1(@RequestBody VmsClassifyCodePageDto dto) {
        log.info("{}税收分类编码导出：税收分类编码：{}，商品名称：{}，商品简称：{}", LOG_MSG, dto.getSphfwssflhbbm(), dto.getSphfwmc(), dto.getSphfwfljc());
        classifyCodeService.exportCode( dto.getSphfwssflhbbm(), dto.getSphfwmc(), dto.getSphfwfljc(), response);
    }

    @ApiOperation("税收分类-获取记录详情导出")
    @AnonymousAccess(value = "/exportDetailList", method = RequestMethod.POST, permissionCode = "VMS_CLASS_IFY_CODE_EXPORT")
    @Log(value = "税收分类-获取记录详情导出", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXATION_CLASSIFICATION_CODE)
    public void exportDetailList(@RequestParam("id") String id, HttpServletResponse response) {
        log.info("{}税收分类-获取记录详情导出{}", LOG_MSG, id);
        classifyCodeService.exportDetailList(id, response);
    }

    @ApiOperation("根据税收分类编码获取行业信息列表")
    @AnonymousAccess(value = "/getHyListByCode", method = RequestMethod.POST, permissionCode = "VMS_TAXCLASS")
    @Log(value = "根据税收分类编码获取行业信息列表", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXATION_CLASSIFICATION_CODE)
    public Result<List<VmsClassifyCodeHyVO>> getHyListByCode(@RequestBody JSONObject jsonObject) {
        if (jsonObject == null || StringUtils.isBlank(jsonObject.getString("id"))) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }
        log.info("{}税收分类-根据税收分类编码获取行业信息列表id{}", LOG_MSG, jsonObject.getString("id"));
        return industryService.getHyListByCode(jsonObject.getString("id"));
    }
}

